from django.db import models

from drf_admin_base.utils.models import BaseModel


class Permissions(BaseModel):
    """
    权限
    """
    method_choices = (
        (u'POST', u'增'),
        (u'DELETE', u'删'),
        (u'PUT', u'改'),
        (u'PATCH', u'局部改'),
        (u'GET', u'查')
    )

    name = models.CharField(max_length=255, verbose_name='权限名')
    method = models.CharField(max_length=10, blank=True, default='', choices=method_choices,
                              verbose_name='方法')  # 值必须是 method_choices 中定义的选项之一
    sign = models.CharField(max_length=255, unique=True, verbose_name='权限标识')
    is_menu = models.BooleanField(verbose_name='是否为菜单')  # True为菜单,False为接口
    route = models.CharField(max_length=255, blank=True, default='', verbose_name='路由')
    parent_id = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父权限')
    description = models.CharField(max_length=255, blank=True, default='', verbose_name='权限描述')

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'system_permissions'
        verbose_name = '权限'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Roles(BaseModel):
    """
    角色
    """
    name = models.CharField(max_length=32, unique=True, verbose_name='角色')
    description = models.CharField(max_length=255, blank=True, default='', verbose_name='描述')
    is_enable = models.BooleanField(verbose_name='启用')

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'system_roles'
        verbose_name = '角色'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Departments(BaseModel):
    """
    组织架构 部门
    """
    name = models.CharField(max_length=50, unique=True, verbose_name='部门')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父部门')
    description = models.CharField(max_length=255, blank=True, default='', verbose_name='描述')

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        managed = False
        db_table = 'system_departments'
        verbose_name = '部门'
        verbose_name_plural = verbose_name
        ordering = ['id']


class UsersRoles(models.Model):
    """用户角色"""
    user = models.ForeignKey('oauth.Users', on_delete=models.DO_NOTHING, db_column='users_id')
    role = models.ForeignKey(Roles, on_delete=models.DO_NOTHING, db_column='roles_id')

    objects = models.Manager()

    class Meta:
        managed = False
        db_table = 'oauth_users_roles'
        verbose_name = '用户角色'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return f"{self.user.name} - {self.role.name}"


class RolesPermissions(models.Model):
    """角色权限"""
    role = models.ForeignKey(Roles, on_delete=models.DO_NOTHING, db_column='role_id')
    permission = models.ForeignKey(Permissions, models.DO_NOTHING, db_column='permission_id', db_constraint=False)

    objects = models.Manager()

    class Meta:
        managed = False
        db_table = 'system_roles_permissions'
        verbose_name = '角色权限'
        verbose_name_plural = verbose_name
        ordering = ['role_id']
